/*
    Project Milestone 3
    by Lorenzo Miori [Lorenzo.Miori@stud-inf.unibz.it,
    Matteo Angriman [Matteo.Angriman@stud-inf.unibz.it],
    Nick Preda [Nicolae.Preda@stud-inf.unibz.it]
*/

package todoGUI;

import DB.*;
import GUI.AddressListGUI;
import GUI.BasicManageGUI;
import GUI.DataModifiedListener;
import java.util.HashMap;

/**
 *
 * @author lorenzo
 */
public class ManageCustomerGUI extends BasicManageGUI {

    /**
     * Creates new form ManageCustomerGUI
     */
    public ManageCustomerGUI(DataModifiedListener changeListener, HashMap<String, String> defaultEntry) {
        super(changeListener, defaultEntry);
        initComponents();
        setTitle("Customer Entry", "Edit customer's details");
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        mainAreaPanel.add(fieldPanel);
        fillDataFields();
        finalSetup();
    };
    
    @Override
    protected void refreshCurrentData() {
        HashMap<String, String> currentEntry = getCurrentData();
        /* ID stays the same */
        currentEntry.put(CustomerDB.CUSTOMER_NAME, nameFieldText.getText());
        currentEntry.put(CustomerDB.CUSTOMER_RESIDENCE_ADDRESS, ""+addressComboBox.getSelectedIndex());
        currentEntry.put(CustomerDB.CUSTOMER_IDCARD_NUMBER, idCardField.getText());
        currentEntry.put(CustomerDB.CUSTOMER_AGE, ""+ageField.getValue());
        if (feminineGender.isSelected()) {
            currentEntry.put(CustomerDB.CUSTOMER_GENDER, "F");
        }
        if (masculineGender.isSelected()) {
            currentEntry.put(CustomerDB.CUSTOMER_GENDER, "M");
        }
        

    }
    
    private void fillDataFields() {
        
        addressComboBox.removeAllItems();
        HashMap<String, String> currentEntry = getCurrentData();
        if (currentEntry == null) {
            return;
        }
        /* Fill the drop menu */
        int addrIndex = 0;
        int i = 0;
        for (HashMap<String, String> addr : new AddressDB(DatabaseLogin.getLastConnection()).getTableData() ) {
            addressComboBox.addItem(AddressDB.formatAddress(addr));
            if (addr.get(AddressDB.ADDRESS_ID).compareTo(nullSentinel(currentEntry.get(CustomerDB.CUSTOMER_RESIDENCE_ADDRESS))) == 0)
                addrIndex = i;
            i++;
        }
        
        /* NOTE: if this is a new entry, value may be null!! */
        uniqueIDLabel.setText("ID: "+nullSentinel(currentEntry.get(CustomerDB.CUSTOMER_ID)));
        nameFieldText.setText(nullSentinel(currentEntry.get(CustomerDB.CUSTOMER_NAME)));
        ageField.setValue(Integer.parseInt(
                                    nullSentinelInteger(
                                    currentEntry.get(CustomerDB.CUSTOMER_AGE))));
        addressComboBox.setSelectedIndex(addrIndex);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        genderButtonGroup = new javax.swing.ButtonGroup();
        fieldPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        masculineGender = new javax.swing.JRadioButton();
        feminineGender = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        idCardField = new javax.swing.JTextField();
        ageField = new javax.swing.JFormattedTextField();
        addressLabel = new javax.swing.JLabel();
        addressComboBox = new javax.swing.JComboBox();
        addAddressButton = new javax.swing.JButton();
        nameFieldText = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        uniqueIDLabel = new javax.swing.JLabel();

        jLabel1.setText("ID Card");

        jLabel2.setText("Gender");

        genderButtonGroup.add(masculineGender);
        masculineGender.setText("M");

        genderButtonGroup.add(feminineGender);
        feminineGender.setText("F");

        jLabel3.setText("Age");

        ageField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));

        addressLabel.setText("Address");

        addressComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        addAddressButton.setText("Add...");
        addAddressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAddressButtonActionPerformed(evt);
            }
        });

        nameLabel.setText("Name");

        uniqueIDLabel.setText("ID: ?");

        javax.swing.GroupLayout fieldPanelLayout = new javax.swing.GroupLayout(fieldPanel);
        fieldPanel.setLayout(fieldPanelLayout);
        fieldPanelLayout.setHorizontalGroup(
            fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 475, Short.MAX_VALUE)
            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(fieldPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(fieldPanelLayout.createSequentialGroup()
                            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(uniqueIDLabel)
                                .addComponent(addressLabel)
                                .addComponent(nameLabel))
                            .addGap(37, 37, 37)
                            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nameFieldText)
                                .addComponent(addressComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(addAddressButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(fieldPanelLayout.createSequentialGroup()
                            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addGroup(fieldPanelLayout.createSequentialGroup()
                                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addComponent(jLabel3))
                                    .addGap(44, 44, 44)
                                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(ageField, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(fieldPanelLayout.createSequentialGroup()
                                            .addComponent(masculineGender)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(feminineGender))
                                        .addComponent(idCardField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(0, 0, Short.MAX_VALUE)))
                    .addContainerGap()))
        );
        fieldPanelLayout.setVerticalGroup(
            fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 203, Short.MAX_VALUE)
            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(fieldPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(uniqueIDLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nameLabel)
                        .addComponent(nameFieldText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(10, 10, 10)
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addressComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addressLabel))
                        .addComponent(addAddressButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(idCardField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(masculineGender)
                        .addComponent(feminineGender))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(ageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fieldPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(347, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fieldPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(235, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addAddressButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAddressButtonActionPerformed
        setEnabled(false);
        new AddressListGUI(this);
    }//GEN-LAST:event_addAddressButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAddressButton;
    private javax.swing.JComboBox addressComboBox;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JFormattedTextField ageField;
    private javax.swing.JRadioButton feminineGender;
    private javax.swing.JPanel fieldPanel;
    private javax.swing.ButtonGroup genderButtonGroup;
    private javax.swing.JTextField idCardField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton masculineGender;
    private javax.swing.JTextField nameFieldText;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel uniqueIDLabel;
    // End of variables declaration//GEN-END:variables
}
